package com.gofaraway.service.student.左神算法课;

import com.gofaraway.service.utils.AlgorithmUtils;

import java.util.Arrays;

/**
 * @author ChenPeng
 * @Date 2024/12/23 22:48
 */
public class Code01_BSExist {

    /**
     * 二分查找 实现
     *
     * @param arr
     * @param value
     * @return
     */
    public static boolean find(int[] arr, int value) {
        if (arr == null || arr.length == 0) {
            return false;
        }
        int L = 0;
        int R = arr.length - 1;
        while (L <= R) {
            int mid = (L + R) / 2;
            if (arr[mid] == value) {
                return true;
            } else if (arr[mid] < value) {
                L = mid + 1;
            } else {
                R = mid - 1;
            }
        }
        return false;
    }

    /**
     * 用二分查找 找到 大于等于num 的最左边的位置
     * arr有序
     * 与leetcode原题一样，只是题目描述不一样
     * https://leetcode.cn/problems/search-insert-position/submissions/628820377/?envType=study-plan-v2&envId=top-100-liked
     *
     * @param arr
     * @param num
     * @return
     */
    public static int mostLeftNoLessNumIndex(int[] arr, int num) {
        if (arr == null || arr.length == 0) {
            return -1;
        }

        int L = 0;
        int R = arr.length - 1;
        int ans = -1;

        while (L <= R) {
            int mid = (L + R) /2;
            if (arr[mid] >= num) {
                ans = mid;
                R = mid -1;
            } else {
                L = mid + 1;
            }
        }
        return ans;

    }


    /**
     * 用二分查找 找到 小于等于num 的最右边的位置
     * arr有序
     *
     * @param arr
     * @param num
     * @return
     */
    public static int mostRightNoLessNumIndex(int[] arr, int num) {
        if (arr == null || arr.length == 0) {
            return -1;
        }

        int L = 0;
        int R = arr.length - 1;
        int ans = -1;

        while (L <= R) {
            int mid = (L + R) / 2;
            if (arr[mid] <= num) {
                ans = mid;
                L = mid + 1;
            } else {
                R = mid - 1;
            }
        }
        return ans;
    }


    public static int test3(int[] arr, int value) {
        for (int i = arr.length - 1; i >=0; i--) {
            if (arr[i] <= value) {
                return i;
            }
        }
        return -1;
    }

    public static int test2(int[] arr, int value) {

        for (int i = 0; i < arr.length; i++) {
            if (arr[i] >= value) {
                return i;
            }
        }
        return -1;
    }

    public static boolean test1(int[] arr, int value) {
        for (int i : arr) {
            if (i == value) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        int testTims = 10000;//测试次数
        int maxSize = 20;//最大测试容量
        int maxNum = 20;//最大测试数据
        boolean euqals = true;
        for (int i = 0; i < testTims; i++) {
            int[] arr1 = AlgorithmUtils.generateRandomArray(maxSize, maxNum);
            Arrays.sort(arr1);
            int value = (int) (Math.random() * maxNum);
            //数值相同内存地址完全没关系，请看copyArray()方法实现
            int ans = mostRightNoLessNumIndex(arr1, value);
            int test = test3(arr1, value);
            if (ans != test) {//比较是否相同
                euqals = false;//一旦有不一样的值就设为false;
                break;
            }
        }
        System.out.println(euqals ? "Success:恭喜你！没毛病！" : "Error：抱歉，有错误");//没错就恭喜，有错就报错
    }

}
